<?php 
	include('../defines.php');
	FunctionsHelper::includeFiles(
		array(
			ADMINISTRATOR_CONTROLLER_DIRECTORY . 'users.php'
		)
	);


	$users = new Users();
	$userSelected = $users->getLoggedUser();

	$invalid = 0;
	$username = $userSelected->getUsername();
	if (isset($_POST['txtUsername']) && isset($_POST['txtPassword']) || !empty($username)){
		if (!empty($_POST['txtUsername']) && !empty($_POST['txtPassword']))
			$arrUser = array ('txtUsername' => $_POST['txtUsername'], 'txtPassword' => $_POST['txtPassword']);
		else 
			$arrUser = array ('txtUsername' => $userSelected->getUsername(), 'txtPassword' => $userSelected->getPassword());

		$userSelected = $users->userExists($arrUser);
		$UserId = $userSelected->getUserId();
		if (empty($UserId)){
			$invalid = 1;
		}else{	
			$users->setLoggedUser($userSelected);
			header('Location: ' . ADMINISTRATOR_VIEW_PATH . 'home.php');
		}
	}

	FunctionsHelper::includeTop();

?>

<script type="text/javascript">

	$(function(){
		$('#frmLogin').validate();
	});
	
</script>
<div class="withBorder" id="main">
	<div id="divLogin" class="formLogin">
		<form name="frmLogin" id="frmLogin" method="post">
			<div>
				<?php 
				if ($invalid == 1){
				?>
				<div class="fullSize bgBlack error">Usuario inv&aacute;lido</div>
				<?php 
				}
				?>
				<br />
				<div class="divCenter divLoginField">
					<div class="loginDescription">Usuario</div><input type="text" id="txtUsername" name="txtUsername" class="required" />
				</div>
				<br />
				<div class="divCenter divLoginField">
					<div class="loginDescription">Contrase&ntilde;a</div><input type="password" id="txtPassword" name="txtPassword" class="required" />
				</div>
				<br />
				<div id="button"><input type="submit" name='sbmSubmit' id='sbmSubmit' value='Ingresar' /></div>
				<br />
			</div>
		</form>
	</div>
</div>
<?php 
	FunctionsHelper::includeBottom();
?>